package com.project.admin.oasign;

import android.Manifest;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Process;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import com.project.admin.oasign.x5webview.X5WebView;

public class MainActivity extends AppCompatActivity {

    // 前段页面 修改这个
//    private final String url = "http://www.besteasylearn.com/teach_web/#/entry";
//    private final String url = "file:///android_asset/test.html";

//    private final String url = "http://192.168.0.104:8083/#/";

    //private final String url = "http://223.68.159.69:81/#/entry";

    private final String url = "http://101.132.120.193:1005/#/showCourse/53a156dd-2438-4d71-977f-2577552c81a9";


    private X5WebView mWebView;
    public static final int REQUEST_CODE = 0x001;

    //配置需要取的权限
    static final String[] PERMISSION = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA,
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideBottomNav();
        setContentView(R.layout.activity_main);
        //请求权限
        ActivityCompat.requestPermissions(this, PERMISSION, REQUEST_CODE);

        mWebView = findViewById(R.id.webview);

        mWebView.addJavascriptInterface(new JsInterface(), "NativeApp");
        mWebView.loadUrl(url);
    }

    public  void hideBottomNav() {
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(0);
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN |
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
    }

    private class JsInterface {

        @JavascriptInterface
        public void exitApp() {
            finish();
            Process.killProcess(Process.myPid());
        }

        @JavascriptInterface
        public void hideBottomNav(){
            hideBottomNav();
        }

        @JavascriptInterface
        public void openApp() {
            try {
                Intent intent = new Intent();
                ComponentName cmp = new ComponentName("com.ahxhjy.xhpad", "com.ahxhjy.xhpad.MainActivity");
                intent.setAction(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_LAUNCHER);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setComponent(cmp);
                startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(MainActivity.this, "本机没有宣毫书法派App，暂不能打开！！！", Toast.LENGTH_SHORT).show();
            }

        }


        @JavascriptInterface
        public void openBrowser() {
            Uri uri = Uri.parse("https://www.baidu.com");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }

        @JavascriptInterface
        public void openSetting() {
            Intent intent = new Intent(Settings.ACTION_SETTINGS);
            startActivity(intent);
        }

        @JavascriptInterface
        public void openFolder() {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("*/*");//设置类型，我这里是任意类型，任意后缀的可以这样写。
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            startActivity(intent);
        }

    }

//    @Override
//    public void onBackPressed() {
//        mWebView.loadUrl("javascript:androidBack()");
////        super.onBackPressed();
//    }


    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN)
            mWebView.loadUrl("javascript:android2Code(" + event.getKeyCode() + ")");
        return false;
//        return super.dispatchKeyEvent(event);
    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
//            mWebView.goBack();
//            return true;
//        }
//        return super.onKeyDown(keyCode, event);
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mWebView.destroy();
        mWebView = null;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //权限申请结果
        if (requestCode == REQUEST_CODE) {
            for (int index = 0; index < permissions.length; index++) {
                switch (permissions[index]) {
                    case Manifest.permission.CAMERA:
                        if (grantResults[index] == PackageManager.PERMISSION_GRANTED) {

                        } else if (grantResults[index] == PackageManager.PERMISSION_DENIED) {
                            /**用户拒绝了权限*/
                            Toast.makeText(this, "应用没有授予权限，请授权！", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(this, "应用没有授予权限，请授权！", Toast.LENGTH_SHORT).show();
                        }
                        break;
                }
            }
        }
    }


}
